package com.lhn.controller;

import com.lhn.entity.User;
import com.lhn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author lhn
 * @date 2024-10-11 23:03
 */

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("findAll/{index}/{limit}")
    public List<User> findAll(@PathVariable("index") int index, @PathVariable("limit") int limit){
        return userService.findAll(index, limit);
    }

    @GetMapping("findById/{id}")
    public User findById(@PathVariable("id") long id){
        return userService.findById(id);
    }

    @GetMapping("count")
    public int count(){
        return userService.count();
    }

    @DeleteMapping("deleteById/{id}")
    public void deleteById(@PathVariable("id") long id){
        userService.deleteById(id);
    }

    @PostMapping("save")
    public void save(@RequestBody User user){
        user.setRegisterdate(new Date());
        userService.save(user);
    }

    @PutMapping("update")
    public void update(@RequestBody User user){
        userService.update(user);
    }
}
